//
//  AppController.m
//  Amber
//
//  Created by Keith Duncan on 06/08/2006.
//  Copyright 2006 thirty-three. All rights reserved.
//

#import "ApplicationController.h"

#import "AFLibrary.h"
#import "iTunesNode.h"
#import "iTunesLibrary.h"

#import "AFSourceList.h"

#import "AFLocateLibraryWindowController.h"

static NSString *const LibraryLocationDefaultsKey = @"libraryPath";

@interface ApplicationController () <AFLibraryDelegate>
@property (readwrite, retain) AFLibrary *library;
@end

@implementation ApplicationController

@synthesize library;
@synthesize sidebar;
@synthesize libraryMenuButton;
@synthesize playlistsController;

- (void)dealloc {
	self.library = nil;
	
	[super dealloc];
}

- (void)awakeFromNib {
	NSArray *sortDescriptors = [NSArray arrayWithObjects:
								[[[NSSortDescriptor alloc] initWithKey:@"type" ascending:NO] autorelease],
								[[[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES] autorelease],
								[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease],
								nil];
	
	[self.playlistsController setSortDescriptors:sortDescriptors];
	
	[self.sidebar bind:NSContentBinding toObject:playlistsController withKeyPath:@"arrangedObjects" options:nil];
	[self.sidebar bind:NSContentValuesBinding toObject:playlistsController withKeyPath:@"arrangedObjects.name" options:nil];
	[self.sidebar bind:AFContentImagesBinding toObject:playlistsController withKeyPath:@"arrangedObjects.image" options:nil];
	[self.sidebar bind:AFSelectionIndexPathBinding toObject:playlistsController withKeyPath:@"selectionIndexPath" options:nil];
	
#if DEBUGFULL
	[self load:nil];
#endif
	
	[[[self sidebar] window] makeFirstResponder:[self sidebar]];
}

- (IBAction)load:(id)sender {
	NSString *loadPath = [[NSUserDefaults standardUserDefaults] stringForKey:LibraryLocationDefaultsKey];
	NSURL *loadLocation = (loadPath != nil ? [NSURL fileURLWithPath:loadPath] : nil);
	
	BOOL request = ((([[[NSApplication sharedApplication] currentEvent] modifierFlags] & NSAlternateKeyMask) == NSAlternateKeyMask) || (loadLocation == nil));
	
	self.library = [[[iTunesLibrary alloc] initWithLocation:loadLocation shouldRequest:request] autorelease];
	self.library.delegate = self;
	
	NSError *error = nil;
	if (![self.library loadAndReturnError:&error]) {
		if (error != nil) [[NSApplication sharedApplication] presentError:error];
		
		self.library = nil;
		return;
	}
	
	[self.libraryMenuButton setMenu:[self.library sourceMenu]];
	
	[[NSUserDefaults standardUserDefaults] setObject:[self.library.location path] forKey:LibraryLocationDefaultsKey];
}

- (NSURL *)libraryRequestLocationFromUser:(AFLibrary *)library {
	AFLocateLibraryWindowController *controller = [[[AFLocateLibraryWindowController alloc] initWithNibName:@"AFSourceListLibraryLocator" bundle:nil] autorelease];
	controller.expectedLocation = [self.library.location path];
	controller.library = self.library;
	
	return [controller requestLibraryLocation];
}

@end

@implementation ApplicationController (Delegate)

- (BOOL)sourceList:(AFSourceList *)view canSelectObject:(iTunesNode *)node {
	if (node.tag == kiTunesFolder && [[node representedObject] objectForKey:@"Playlist Items"] == nil) return NO;
	else if (node.tag == kiTunesLibrary && [node.name isEqualToString:@"Podcasts"]) return NO;
	return YES;
}

- (BOOL)library:(AFLibrary *)library isObjectSelectable:(id)object {
	return [self sourceList:nil canSelectObject:object];
}

- (NSBundle *)bundle {
	return [NSBundle bundleForClass:[self class]];
}

@end

@implementation ApplicationController (DataSource)

- (void)sourceList:(AFSourceList *)view shouldDisplayDisclosure:(BOOL *)showDisclosure forObject:(id)object {
	if (object == [[self.library.rootNode childNodes] lastObject]) *showDisclosure = YES;
}

@end
